package br.com.fatecpg.minhasfinancas;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import br.com.fatecpg.minhasfinancas.model.AlertaModel;
import br.com.fatecpg.minhasfinancas.model.DespesaModel;
import br.com.fatecpg.minhasfinancas.sqlite.AlertaSqlite;
import br.com.fatecpg.minhasfinancas.sqlite.DespesaSqlite;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;

public class AlertaActivity extends Activity {

	AlertaSqlite db = new AlertaSqlite(this);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alerta);
		ListView lista = (ListView) findViewById(R.id.lstCartoes);
		try {
			ArrayAdapter<AlertaModel> adapter;

			adapter = new ArrayAdapter<AlertaModel>(this,
					android.R.layout.simple_list_item_1, db.getListaAlerta());

			lista.setAdapter(adapter);

			Button bt = (Button) findViewById(R.id.btnSalvarAlerta);

			bt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Salvar(v);
				}
			});

			Button btnVoltar = (Button) findViewById(R.id.btnVoltar);
			btnVoltar.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					gotoMenu(v);
				}
			});

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void gotoMenu(View v) {
		Intent intent = new Intent();
		intent.setClass(this, MainActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.alerta, menu);
		return true;
	}

	private void Salvar(View v) {

		boolean alerta = false;

		EditText descricao = (EditText) findViewById(R.id.txtDescricao);
		EditText data = (EditText) findViewById(R.id.txtData);
		EditText diasAntecendencia = (EditText) findViewById(R.id.txtDiasAntecedencia);

		String dataStr = data.getText().toString();
		DateFormat fmt = new SimpleDateFormat("dd/MM/yyyy");
		java.sql.Date dataSQL;
		try {
			dataSQL = new java.sql.Date(fmt.parse(dataStr).getTime());
		} catch (ParseException e) {
			dataSQL = (java.sql.Date) new Date();	
		}

		AlertaModel model = new AlertaModel();

		model.setData(dataSQL);
		model.setDescricao(descricao.getText().toString());
		model.setDiasAntecedencia(Integer.parseInt(diasAntecendencia.getText()
				.toString()));

		db.Insert(model);

	}

}
